package com.example.spider.provider.why10w.deprecated.wgwhy10;

import cn.hutool.core.util.ReUtil;
import com.example.spider.provider.why10w.AbstractWhy10wDetailResultHandler;
import com.example.spider.provider.why10w.dto.Why10wDetailTask;
import com.example.spider.provider.why10w.dto.WhyImageInfo;
import com.example.spider.provider.why10w.dto.WhyTaskResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理 wgwhy10 详情页结果
 *
 * @author lym
 */
@Slf4j
//@Component
public class Wgwhy10DetailResultHandler extends AbstractWhy10wDetailResultHandler {

    private static final String adImageUrl = "https://www.10wgwhy.com/zb_users/upload/9b9a3f4bccf0780e.jpg";

    /**
     * handle
     */
    @Override
    public void doHandle(Why10wDetailTask task, String html) {
        // 要解析的太多了，先截取期望部分，避免每次解析全部html
        String hasContentRegex = "(<div class=\"post\">.*?)<div class=\"post_comments\">";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, html);
        if (contentStr.isEmpty()) {
            // 结束了
            log.error("why10Detail invalid. Task:{}", task);
            return;
        }

        String content = contentStr.get(0);


        String id = ReUtil.findAllGroup1("https://www.10wgwhy.com/.\\w+?/(.*?).html", task.getUrl()).get(0);

        String title = task.getTitle();

        String category = task.getFirstCategory();

        List<String> tagList = ReUtil.findAllGroup1("<a href=\"https://www.10wgwhy.com/tags-\\d+.html\">(.*?)</a>", content);

        String contentRegex = "<div class=\"article_content\">(.*?)<div style=\"text-align:center;width:100%;height:auto;\">";
        List<String> contentFindResult = ReUtil.findAllGroup1(contentRegex, content);
        String realContent = "";
        if (CollectionUtils.isEmpty(contentFindResult)) {
            // 不存在
            log.error("empty content");
        } else {
            realContent = contentFindResult.get(0).trim();
        }

        // 解析文章图片
        List<WhyImageInfo> imageInfoList = new ArrayList<>();
        List<String> imageMatchList = ReUtil.findAllGroup1("<img (.*?)>", realContent);
        if (!CollectionUtils.isEmpty(imageMatchList)) {
            for (String s : imageMatchList) {
                String name = ReUtil.findAllGroup1("alt=\"(.*?)\"", s).get(0);
                String url = ReUtil.findAllGroup1("src=\"(.*?)\"", s).get(0);
                if(adImageUrl.equals(url)){
                    // 去掉广告
                    continue;
                }
                WhyImageInfo imageInfo = WhyImageInfo.builder()
                        .name(name)
                        .url(url)
                        .build();
                imageInfoList.add(imageInfo);
            }
        }

        WhyTaskResult result = WhyTaskResult.builder()
                .id(id)
                .title(title)
                .url(task.getUrl())
                .categoryList(List.of(Wgwhy10CategoryEnum.valueOf(category).getCategory()))
                .tagList(tagList)
                .imageInfoList(imageInfoList)
                .content(realContent)
                .source(task.getSource())
                .build();

        // ***********************

        // 保存到文件
        repository.addResult(result);

        repository.saveHtml(task, html);
    }
}
